import base64
import qrcode
from django.utils.six import BytesIO
from django.http import HttpResponse
import json

def make_qrcode(data):
    img = qrcode.make(data)
    buf = BytesIO()
    img.save(buf)
    image_stream = buf.getvalue()
    return HttpResponse(image_stream, content_type="image/png")

# 加密
def encryption(data):
    return base64.encodebytes(data.encode('utf-8'))

# 解密
def decryption(data):
    return base64.decodebytes(data).decode('utf-8')

# 获取图书二维码
def get_book_qrcode(book):
    book_info = {
        "id": book.id,
        "name": book.name,
        "author": book.author,
    }
    book_info = encryption( json.dumps(book_info) )
    return qrcode.make(book_info)